<?php

namespace app\apisell\service\user;

use app\common\utils\Res;
use think\facade\Db;
use think\facade\Log;

class UserService
{
    public static function list($param)
    {
        // 设置默认分页大小
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        // 计算分页偏移量
        $offset = ($param['pageNumber'] - 1) * $limit;
        // 初始化查询条件数组
        $map = [];
        // 根据标签筛选
        if (isset($param["phone"]) && !empty($param["phone"])) {
            $map["phone"] = $param["phone"];
        }
        if (isset($param["is_popu_lever"]) && !empty($param["is_popu_lever"])) {
            $map["is_popu_lever"] = $param["is_popu_lever"];
        }
        $result = Db::table('xh_sell_user')->where($map)->order('add_time desc')->limit($offset, $limit)->select()->toArray();
        foreach ($result as $item=> $v){
            if ($v['is_popu_lever'] == 0){
                $result[$item]['popu_lever'] = '普通用户';
            }elseif ($v['is_popu_lever'] == 1){
                $result[$item]['popu_lever'] = '一级推广员';
            }elseif ($v['is_popu_lever'] == 2){
                $result[$item]['popu_lever'] = '二级推广员';
            }
            
        }
        // 获取总记录数
        $total = self::pageUserCount($map);
        // 组装返回数据
        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
        
    }
    public static function pageUserCount($map)
    {
        $count = Db::table('xh_sell_user')->where($map)->count();
        return $count;
    }




}
